package com.premnirmal.magnet.library;

import cs.help.tool.Log;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.Component;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.agp.window.service.Window;

import java.lang.ref.WeakReference;
import java.math.BigDecimal;
import java.util.HashMap;

/**
 * Created by prem on 7/20/14.
 * A class that takes care of animating a view in a simple way.
 */
public class SimpleAnimator {

    protected WeakReference<Component> viewRef;
    protected Window window;

    public final static int IN = 0;
    public final static int OUT = 1;

    private SimpleAnimator(Component view) {
        this.viewRef = new WeakReference<>(view);
        createAnimal();
    }

    public void setWindow(Window window) {
        this.window = window;
    }

    private static volatile HashMap<Component, SimpleAnimator> instance;

    public static SimpleAnimator getInstance(Component view) {
        HashMap<Component, SimpleAnimator> lockInstance = instance;
        if (lockInstance == null) {
            synchronized (SimpleAnimator.class) {
                lockInstance = instance;
                if (lockInstance == null) {
                    lockInstance = instance = new HashMap<Component, SimpleAnimator>();
                }
            }
        }
        SimpleAnimator simpleAnimator = lockInstance.get(view);
        if (simpleAnimator == null) {
            simpleAnimator = new SimpleAnimator(view);
            lockInstance.put(view, simpleAnimator);
        }
        return simpleAnimator;
    }

    AnimatorValue animatorValue;

    public void createAnimal() {
        animatorValue = new AnimatorValue();
        animatorValue.setDuration(500);
    }

    public void setAnimationStateChangeListener(MiStateChangeListener stateChangeListener) {
        if (animatorValue != null) {
            animatorValue.setStateChangedListener(stateChangeListener);
        }
    }


    protected void startAnimation(int animation) {
        if (animatorValue.isRunning()) {
            animatorValue.stop();
        }

        animatorValue.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                try {
                    Component view = viewRef.get();
                    Display display = DisplayManager.getInstance().getDefaultDisplay(view.getContext()).get();
                    DisplayAttributes attributes = display.getAttributes();
                    DisplayAttributes realAttributes = display.getRealAttributes();
                    Log.i("SimpleAnimator  attributes:width=" + attributes.width + "  height=" + attributes.height + "    realAttributes:width=" + realAttributes.width + "   height=" + realAttributes.height);

                    int dur = window.getLayoutConfig().get().height / 2 + view.getEstimatedHeight() / 2;
                    view.setVisibility(Component.VISIBLE);
                    if (animation == IN) {
                        view.setTranslationY((BigDecimal.valueOf(1).subtract(BigDecimal.valueOf(v))).multiply(BigDecimal.valueOf(dur)).intValue());
                    } else if (animation == OUT) {
                        view.setTranslationY(BigDecimal.valueOf(1).multiply(BigDecimal.valueOf(v)).intValue());
                    }
                } catch (Exception e) {
                    Log.i("SimpleAnimator.onUpdate([animatorValue, v]):96   动画运行出错! " + e.getMessage());
                }
            }
        });
        animatorValue.start();
    }
}
